<?php

namespace app\admin\controller;

class Pub extends Common {

        /**
         * 更新缓存
         */
        public function cache() {
                $this->checkUser();
                if (IS_POST) {
                        //强制类型转换成数组
                        $type = I('post.type/a');
                        //编译缓存
                        if (in_array("temp", $type)) {
                                deldir(TEMP_PATH);
                        }
                        //数据缓存
                        if (in_array("cache", $type)) {
                                deldir(CACHE_PATH);
                                //unlink(RUNTIME_FILE);
                        }
                        //模板缓存
                        if (in_array("html", $type)) {
                                if (!is_dir(HTML_PATH)) {
                                        //unlink(HTML_PATH);
                                } else {
                                        deldir(HTML_PATH);
                                }
                        }

                        $this->iSucess('更新缓存成功');
                } else {
                        return $this->fetch();
                }
        }

        /**
         * 获取地区单选
         */
        public function get_zone_single() {
                $zone = M("Zone");
                $result = $zone->where(array('pid' => 0))->select();
                $html = '';
                $html .= '<ul id="j_select_tree_zonesingle" data-check-enable="true" data-chk-style="radio" data-radio-type="all" class="ztree" data-toggle="ztree" data-expand-all="false" data-on-click="S_NodeClick" data-on-check="S_NodeClickBringBack">';
                foreach ($result as $value) {
                        $html .='<li data-id="' . $value['id'] . '" data-pid="' . $value['pid'] . '">' . $value['name'] . '</li>';
                        $result1 = $zone->where(array('pid' => $value['id']))->select();
                        foreach ($result1 as $v) {
                                $html .='<li data-id="' . $v['id'] . '" data-pid="' . $value['id'] . '">' . $v['name'] . '</li>';
                                $result2 = $zone->where(array('pid' => $v['id']))->select();
                                foreach ($result2 as $val) {
                                        $html .='<li data-id="' . $val['id'] . '" data-pid="' . $v['id'] . '">' . $val['name'] . '</li>';
                                }
                        }
                }

                $html .='</ul>';
                $this->assign('html', $html);
                return $this->fetch();
        }

        /**
         * 获取目的地单选
         */
        public function get_location_single() {
                $zone = M("Location");
                $result = $zone->where(array('pid' => ['in', '1,2']))->select();
                $html = '';
                $html .= '<ul id="j_select_tree_locationsingle" data-check-enable="true" data-chk-style="radio" data-radio-type="all" class="ztree" data-toggle="ztree" data-expand-all="false" data-on-click="S_NodeClick" data-on-check="S_NodeClickBringBack">';
                foreach ($result as $value) {
                        $html .='<li data-id="' . $value['id'] . '" data-pid="' . $value['pid'] . '">' . $value['name'] . '</li>';
                        $result1 = $zone->where(array('pid' => $value['id']))->select();
                        foreach ($result1 as $v) {
                                $html .='<li data-id="' . $v['id'] . '" data-pid="' . $value['id'] . '">' . $v['name'] . '</li>';
                                $result2 = $zone->where(array('pid' => $v['id']))->select();
                                foreach ($result2 as $val) {
                                        $html .='<li data-id="' . $val['id'] . '" data-pid="' . $v['id'] . '">' . $val['name'] . '</li>';
                                }
                        }
                }

                $html .='</ul>';
                $this->assign('html', $html);
                return $this->fetch();
        }

        /**
         * 获取目的地单选
         */
        public function get_location_multi() {
                if (!empty($_REQUEST['id'])) {
                        $ids = $_REQUEST['id'];
                        $ids_arr = explode(',', $ids);
                } else {
                        $ids = '';
                        $ids_arr = array();
                }
                $zone = M("Location");
                $result = $zone->where(array('pid' => ['in', '1,2'],'status'=>'1'))->select();
                $html = '';
                $html .=' <input type="hidden" name="names" id="j_ztree_names_locationmulti"  size="18" readonly >';
                $html .=' <input type="hidden" name="ids" id="j_ztree_menus_locationmulti" data-tree="j_select_tree_locationmulti" value="' . $ids . '"  size="18" readonly >';
                $html .= '<ul id="j_select_tree_locationmulti" data-check-enable="true" class="ztree" data-toggle="ztree" data-expand-all="false" data-check-enable="true" data-on-check="S_NodeCheck" data-on-click="S_NodeClick">';
                foreach ($result as $value) {
                        $html .='<li data-id="' . $value['id'] . '" data-pid="' . $value['pid'];
                        if (in_array($value['id'], $ids_arr)) {
                                $html .='" data-checked="true" ' . ' >' . $value['name'] . '</li>';
                        } else {
                                $html .='" >' . $value['name'] . '</li>';
                        }
                        $result1 = $zone->where(array('pid' => $value['id'],'status'=>'1'))->select();
                        foreach ($result1 as $v) {
                                $html .='<li data-id="' . $v['id'] . '" data-pid="' . $value['id'];
                                if (in_array($v['id'], $ids_arr)) {
                                        $html .='" data-checked="true" ' . ' >' . $v['name'] . '</li>';
                                } else {
                                        $html .='" >' . $v['name'] . '</li>';
                                }
                                $result2 = $zone->where(array('pid' => $v['id'],'status'=>'1'))->select();
                                foreach ($result2 as $val) {
                                        $html .='<li data-id="' . $val['id'] . '" data-pid="' . $v['id'];
                                        if (in_array($val['id'], $ids_arr)) {
                                                $html .='" data-checked="true" ' . ' >' . $val['name'] . '</li>';
                                        } else {
                                                $html .='" >' . $val['name'] . '</li>';
                                        }
                                }
                        }
                }

                $html .='</ul>';
                $this->assign('html', $html);
                return $this->fetch();
        }

        public function get_labels() {
                $ids = I("post.ids/a");
                $model = M("ToursLabel");
                $foo = array();
                //过滤父级
                $pt_ids = M('Location')->where(['pid' => ['in', '1,2']])->field('id')->select();
                $vv = '';
                foreach ($pt_ids as $v) {
                        $vv = $vv . ',' . $v['id'];
                }
                $arr_nd = explode(',', $vv);
                foreach ($ids as $k => $v) {
                        if ($v == 1 || $v == 2 || in_array($v, $arr_nd)) {
                                continue;
                        }
                        $list = $model->where(array("prov_id" => array("like", "%" . $v . "%")))->select();
                        foreach ($list as $val) {
                                if (!empty($list)) {
                                        $foo[$val['id']] = $val;
                                }
                        }
                }
                echo json_encode(array_values($foo));
        }

        public function get_supplier() {
                $map = $this->_search("Supplier");
                if (I('post.name')) {
                        $map['name'] = array('like', "%" . I('post.name') . "%");
                }
                $this->_list(D("Supplier"), $map);
                return $this->fetch();
        }
        
        public function get_label() {
                $map = $this->_search("ToursLabel");
                if (I('post.name')) {
                        $map['name'] = array('like', "%" . I('post.name') . "%");
                }
                $this->_list(D("ToursLabel"), $map);
                return $this->fetch();
        }

        /*
         * 返回标准json格式
         */

        public function get_zone_by_pid() {
                $id = I('get.id');
                if (empty($id))
                        return;
                $zone = M("Location")->field('id,name')->where(array('pid' => $id, 'status' => 1))->select();
                $return = array();
                $return[] = array("", "--请选择--");
                foreach ($zone as $value) {
                        $return[] = array($value['id'], $value['name']);
                }
                echo json_encode($return);
                exit;
        }

        /*
         * @图片上传
         * 【不生成缩略图】
         */

        public function image_upload($path = '') {
                $config = array(
                    'autoSub' => true,
                    'driver' => 'Local',
                    'savePath' => $path . "/"
                );
                $info = $this->_uploadImage($_FILES['file'], $config);
                $data['statusCode'] = '200';
                $data['message'] = '上传图片成功';
                $data['filename'] = '/Uploads' . '/' . $info['savepath'] . $info['savename'];
                return $this->ajaxReturn($data);
        }

        /*
         * @图片上传
         * 【生成缩略图】并删除原图
         */

        public function image_upload_thumb($path = '') {
                $config = array(
                    'autoSub' => true,
                    'driver' => 'Local',
                    'savePath' => $path . "/"
                );
                //缩略图(等比缩放)
                $thumb = array(
                    'maxWidth' => "400,1000",
                    'suffix' => '_s,_b',
                    'picDelete' => true
                );
                $info = $this->_uploadImage($_FILES['file'], $config, $thumb);
                $data['statusCode'] = '200';
                $data['message'] = '上传成功';
                //返回小缩略图
                $data['filename'] ='/Uploads/'.$info['thumb'][1];
                
                return $this->ajaxReturn($data);
        }

}
